/***************************************************************************
 *   Copyright (C) 2006 by Sergejs Melderis   *
 *   sergey.melderis@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "browser.h"
#include "tooltip.h"
#include <khtml_part.h>
#include <kurl.h>
#include <khtmlview.h>
#include <qevent.h>
#include <qapplication.h>
#include <kio/job.h>
#include "prefs.h"
#include <qimage.h>
#include <klocale.h>
#include <khtmlview.h>
#include <qlabel.h>
#include <qtimer.h>
#include <stdlib.h>
#include <qthread.h>        
        //debug
#include <qapplication.h>
BrowserImpl::BrowserImpl(QWidget *parent, const char *name):
    KHTMLPart(parent, name)
{
    connect(browserExtension(),
            SIGNAL(openURLRequest(const KURL&, const KParts::URLArgs&)),
            this, SLOT(openRequestedURL(const KURL &)));

    connect(this, SIGNAL(canceled(const QString&)), this, SLOT(showErrorMessage(const QString&)));

}

BrowserImpl::~BrowserImpl()
{
}

void BrowserImpl::showErrorMessage(const QString &errStr)
{
    begin();
    write("<html><body>");
    write("<p>" + i18n("The following error has occured:  ") + errStr + "</p>");
    write("<p>" + i18n("It seems that you are not connected to internet. "));
    write(i18n("Check you network settings.") + "</p>");
    write("</body></html>");
    end();
}

void BrowserImpl::showError(KIO::Job *job)
{
    // Without killing the job GUI would be unresponsive. Why??
    job->kill();
    // signal canceled() should be emitted now...
}


void BrowserImpl::openRequestedURL(const KURL &url)
{
    if (url.path() != mURL.path())
        return;
    mURL.setQuery(url.query());
    openURL(mURL);
}


void BrowserImpl::khtmlMousePressEvent(khtml::MousePressEvent *event)
{
    QMouseEvent *qevent = event->qmouseEvent();
    ButtonState bst = qevent->button();
    if (bst == Qt::RightButton) {
        QString selText = selectedText();
        emit wordSelected(selText, qevent->globalPos());
    }
    KHTMLPart::khtmlMousePressEvent(event);
}



Browser::Browser(QWidget *parent, BrowserPopupMenu *menu):
        QFrame(parent)
{
    mMenu = menu;
    m_tooltip = new Tooltip(this);
    init();
}


Browser::~Browser()
{
    delete m_tooltip;
}

void Browser::init()
{
    b = new BrowserImpl(this, 0);
    resize(Prefs::windowWidth(), Prefs::windowHeight());
    setMinimumSize(QSize(400, 500));
    b->showProgressInfo(false);
    b->setProgressInfoEnabled(false);
    // do not use dynamical icon for browser window
    // use statical, defined in klingvotranslator.cpp
    setIcon(QPixmap::fromMimeSource("Icon"));

    connect(b, SIGNAL(wordSelected(const QString&, const QPoint &)),
            this, SLOT(showMenu(const QString&, const QPoint &)));
}

void Browser::openPageForQuery(const QString &query)
{
    b->begin();
    b->write("<html><body>");
    b->write("<h3 style=\"background-color : rgb(255,204,102); margin-left : 5px; margin-top : 5px; width : 180px;\">" + i18n("Contacting Lingvo...") + "</h3>");
    b->write("</body></html>");
    b->view()->layout();
    
    b->mURL.setQuery(query);
    b->openURL(b->mURL);
}

void Browser::setURL(const KURL &url)
{
    b->mURL = url;
}

KURL Browser::url() const
{
    return b->url();
}


void Browser::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Escape) {
        close();
        m_tooltip->hide();
    }

}


void Browser::resizeEvent(QResizeEvent *e)
{
    m_tooltip->hide();
    b->view()->resize(e->size());
    if (Prefs::hasSeenIntro()) {
        Prefs::setWindowHeight(height());
        Prefs::setWindowWidth(width());
    }

}

KHTMLPart* Browser::htmlPart()const
{
    return b;
}

void Browser::closeEvent(QCloseEvent *e)
{
    e->accept();
    if (!Prefs::hasSeenIntro()) {
        Prefs::setHasSeenIntro(true);
        resize(Prefs::windowWidth(), Prefs::windowHeight());
    }
    
    emit closed();
}

void Browser::showMenu(const QString &word, const QPoint &point)
{
    lastPoint = point;
    mMenu->setCurrentTranslation(word);
    mMenu->popupMenu()->popup(point);
}

void Browser::showAddWordResult(const QString &result)
{
    QPoint labelPoint = QPoint(lastPoint.x(), lastPoint.y() - 40);

    m_tooltip->display(result, labelPoint);
}

#include "browser.moc"


